package com.gxa.service.impl;

import com.gxa.common.R;
import com.gxa.mapper.RecepitPayMapper;
import com.gxa.pojo.*;
import com.gxa.service.RecepitPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RecepitPayServiceImpl implements RecepitPayService {
    @Autowired(required = false)
    private RecepitPayMapper mapper;
//分页查询客户
    @Override
    public R queryClient(Integer currentPage, Integer limit, String clientType, String msg) {
        int param1 = (currentPage-1)*limit;
        int param2 = limit;
        int count = mapper.queryCount(param1,param2,clientType,msg);
        List<Customer> customers = mapper.queryClientByMsg(param1,param2,clientType,msg);
        List list = new ArrayList();
        list.add(count);
        list.add(customers);
        R r = new R();
        r.setCode(200);
        r.setMsg("success");
        r.setData(list);
        return r;
    }
//查询客户种类
    @Override
    public R queryKHType() {
        List<Map> map = mapper.queryKHType();
        R r = new R();
        r.setCode(200);
        r.setData(map);
        r.setMsg("success");
        return r;
    }
//查询结算账户
    @Override
    public R queryJSType() {
        List<Map> map = mapper.queryJSType();
        R r = new R();
        r.setData(map);
        r.setCode(200);
        r.setMsg("success");
        return r;
    }
//查询结算方式
    @Override
    public R queryPayType() {
        List<Map> list = mapper.queryPayType();
        R r = new R();
        r.setCode(200);
        r.setData(list);
        r.setMsg("success");
        return r;
    }
//分页查询供应商列表
    @Override
    public R queryGYSList(Map map) {
        Integer count =  mapper.queryGYSListCount(map);
        List<Supplier> list = mapper.queryGYSList(map);
        List list1 = new ArrayList();
        list1.add(count);
        list1.add(list);
        R r = new R();
        r.setCode(200);
        r.setData(list1);
        r.setMsg("success");
        return r;
    }
//查询供应商类别
    @Override
    public R queryGYSType() {
        List<Map> list = mapper.queryGYSType();
        R r = new R();
        r.setData(list);
        r.setMsg("success");
        r.setCode(200);
        return r;
    }
//查询源单
    @Override
    public R queryYDList(Map map) {
        Integer status = (Integer)map.get("status");
        R r = new R();
        //1代表是收款单页面的请求，2代表付款单的请求
        if(status==1){
            Integer skdCount = mapper.querySkdYd(map);
            List<Receipt_Sales> receipt_sales = mapper.querySkdYdList(map);
            List list  = new ArrayList();
            list.add(skdCount);
            list.add(receipt_sales);
            r.setCode(200);
            r.setMsg("success");
            r.setData(list);
        }
        if(status==2){
            Integer fkdCount = mapper.queryFkdYd(map);
            List<Receipt_Purchase> receipt_purchase = mapper.queryFkdYdList(map);
            List list  = new ArrayList();
            list.add(fkdCount);
            list.add(receipt_purchase);
            r.setCode(200);
            r.setMsg("success");
            r.setData(list);
        }
        return r;
    }
//查询收入类别
    @Override
    public R querySRType() {
        List<Map> list = mapper.querySRType();
        R r = new R();
        r.setCode(200);
        r.setMsg("success");
        r.setData(list);
        return r;
    }
//查询支出类别
    @Override
    public R queryZCType() {
        List<Map> list = mapper.queryZCType();
        R r = new R();
        r.setData(list);
        r.setMsg("success");
        r.setCode(200);
        return r;
    }
//查询收付款单
    @Override
    public R querySkdList(Map map) {
        R r = new R();
        Integer type = (Integer)map.get("type");
        //1代表收款单，2代表付款单
        if(type==1){
            Integer skdCount = mapper.querySkdCount(map);
            List<Receipt_Pay> list = mapper.querySkdList(map);
            List list1 = new ArrayList();
            list1.add(skdCount);
            list1.add(list);
            r.setCode(200);
            r.setMsg("success");
            r.setData(list1);
        }else {
            Integer fkdCount = mapper.queryFkdCount(map);
            List<Receipt_Pay> receipt_pay = mapper.queryFkdList(map);
            List list1 = new ArrayList();
            list1.add(fkdCount);
            list1.add(receipt_pay);
            r.setCode(200);
            r.setMsg("success");
            r.setData(list1);
        }
        return r;
    }
//具体数据回显
    @Override
    public R receiptDetails(Integer status , String num) {
        R r = new R();
        if(status==1){//1代表收款单
            //分为两部分操作,第一部分查询页面上半部分，第二部分查询下半页面
            Receipt_Pay receipt_pay = mapper.receiptSkd(num);
            List<Receipt_Sales> receipt_sales = mapper.receiptSales(num);
            Skd skd = new Skd();
            skd.setReceipt_pay(receipt_pay);
            skd.setReceipt_sales(receipt_sales);
            r.setCode(200);
            r.setMsg("success");
            r.setData(skd);
        }else{
            Receipt_Pay receipt_pay = mapper.receiptSkd(num);
            List<Receipt_Purchase> receipt_purchase = mapper.receiptPurchase(num);
            Fkd fkd = new Fkd();
            fkd.setReceipt_pay(receipt_pay);
            fkd.setReceipt_purchase(receipt_purchase);
            r.setCode(200);
            r.setMsg("success");
            r.setData(fkd);
        }
        return r;
    }
//添加收款单
    @Override
    public R addReceiptPaySkd(Skd skd) {
        R r = new R();
        try {
            //将收款单中的内容分类取出来
            Receipt_Pay receipt_pay = skd.getReceipt_pay();
            List<Receipt_Sales> receipt_sales = skd.getReceipt_sales();
            List<Payaccount> payaccounts = receipt_pay.getPayaccounts();
            String receipt_pay_id = receipt_pay.getReceipt_Pay__Id();
            int i = mapper.addReceiptPay(receipt_pay);
            int j = mapper.addPayaccounts(receipt_pay_id,payaccounts);
            int m = mapper.updatereceiptSales(receipt_pay_id,receipt_sales);
            r.setMsg("success");
            r.setCode(200);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            r.setMsg("false");
            r.setCode(500);
            return r;
        }
    }
//添加付款单
    @Override
    public R addReceiptPayFkd(Fkd fkd) {
        R r = new R();
        try {
            List<Receipt_Purchase> receipt_purchase = fkd.getReceipt_purchase();
            Receipt_Pay receipt_pay = fkd.getReceipt_pay();
            String receipt_pay_id = receipt_pay.getReceipt_Pay__Id();
            List<Payaccount> payaccounts = receipt_pay.getPayaccounts();
            int i = mapper.addPayaccounts(receipt_pay_id,payaccounts);
            int j = mapper.addReceiptPayFkd(receipt_pay);
            int m = mapper.updatereceiptPurchase(receipt_pay_id,receipt_purchase);
            r.setCode(200);
            r.setMsg("success");
            return r;
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("false");
            return r;
        }

    }
//添加其他收款单
    @Override
    public R addOtherPay(Otherpay otherpay) {
        R r = new R();
        try {
            List<Receipt_Otherpay> otherpays = otherpay.getOtherpays();
            String otherpay_id = otherpay.getOtherpay_Id();
            int i = mapper.addOtherPay(otherpay);
            int j = mapper.addOtherpayList(otherpay_id,otherpays);
            r.setCode(200);
            r.setMsg("success");
            return r;
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("false");
            return r;
        }
    }
//查询其他收付款单子
    @Override
    public R queryOtherPayList(Integer status) {
        List<Otherpay> otherpay = mapper.queryOtherPayList(status);
        R r = new R();
        r.setCode(200);
        r.setData(otherpay);
        r.setMsg("success");
        return r;
    }
//查询具体的其他收付款
    @Override
    public R qeryDetailsOtherPay(String num) {
        Otherpay otherpay = mapper.qeryDetailsOtherPay(num);
        R r  = new R();
        r.setCode(200);
        r.setData(otherpay);
        return r;
    }

}
